﻿using SIAKADCORE.Helpers;
using SIAKADCORE.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIAKADCORE.Controllers
{
    public static class LoginController
    {
        public static bool CheckPassword(string username, string passwordLama)
        {
            string realPassword = new AesCipherUtility("reintasa", "cantik").Encrypt(passwordLama);
            var dataset = new DataSet();

            string queryString = string.Format("SELECT 1 FROM {0} WHERE {1} = '{2}' AND {3} = '{4}'",
                User.TableName, User.ColumnNameUsername, username, User.ColumnNamePassword, realPassword);

            SQLHelper.Instance.ExecuteReader(queryString, out dataset);

            try
            {
                return dataset.Tables[0].Rows.Count == 1;
            }
            catch
            {
                return false;
            }
        }

        public static bool GantiPassword(string username, string passwordBaru)
        {
            string realPassword = new AesCipherUtility("reintasa", "cantik").Encrypt(passwordBaru);
            string queryString = string.Format("UPDATE {0} SET {1} = '{2}' WHERE {3} = '{4}'",
                User.TableName, User.ColumnNamePassword, realPassword, User.ColumnNameUsername, username);

            return SQLHelper.Instance.ExecuteNonQuery(queryString);
        }

        public static bool Login(string username, string password, out Login login)
        {
            string realPassword = new AesCipherUtility("reintasa", "cantik").Encrypt(password);
            var dataset = new DataSet();

            string queryString = string.Format("SELECT {0} ,{1} ,{2} ,{3} ,{4} ,{5} FROM {6} WHERE {1} = '{7}' AND {2} = '{8}'",
                User.ColumnNameIdUser, User.ColumnNameUsername, User.ColumnNamePassword,
                User.ColumnNameIdRole, User.ColumnNameIdDosen, User.ColumnNameIdMahasiswa, User.TableName,
                username, realPassword);

            bool isExist = UserController.GetTabel(out dataset, queryString);
            if (isExist && dataset.Tables[0].Rows.Count == 1)
            {
                var selected = dataset.Tables[0].Rows[0];
                if (selected[3].ToString() == "RL001")
                {
                    login = new Login("admin", username, LoginType.Admin);
                }
                else if (selected[3].ToString() == "RL002")
                {
                    login = new Login(selected[5].ToString(), username, LoginType.Mahasiswa);
                }
                else if (selected[3].ToString() == "RL003")
                {
                    login = new Login(selected[6].ToString(), username, LoginType.Pegawai);
                }
                else
                {
                    login = new Login(selected[4].ToString(), username, LoginType.Dosen);
                }
                return true;
            }
            else
            {
                login = null;
                return false;
            }
        }
    }
}
